// pages/cancelOrder/cancelOrder.js
const app = getApp()
const toast = require('../../utils/toast.js')
var titleArr = [{
    title: '不想订了',
    id: 0,
    selectImageUrl: '/images/check_gray.png',
    num: 0
  },
  {
    title: '选错了',
    id: 1,
    selectImageUrl: '/images/check_gray.png',
    num: 0
  },
  {
    title: '信息填写错误, 重新下单',
    id: 2,
    selectImageUrl: '/images/check_gray.png',
    num: 0
  },
  {
    title: '支付遇到问题',
    id: 3,
    selectImageUrl: '/images/check_gray.png',
    num: 0
  },
  {
    title: '价格有变化',
    id: 4,
    selectImageUrl: '/images/check_gray.png',
    num: 0
  },
  {
    title: '其他原因',
    id: 5,
    selectImageUrl: '/images/check_gray.png',
    num: 0
  }
];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    soiId: '',
    username: '',
    titleArr: titleArr,
    otherHidden: true,
    // 输入其他原因的内容
    otherContent: '',
    // 原因的输入内容
    // 其他原因输入的字数
    inputCount: 0,
    cancellationReasons: 99,
    isCanCommite: false,
    // 判断是从详情还是订单页面进来的
    orderNum: 0, 
    isShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var phoneNumber = wx.getStorageSync('phoneNumber')
    this.setData({
      orderNum: options.orderNum,
      soiId: options.soiId,
      username: phoneNumber,
      imageUrl: '/images/check_gray.png',
    })
  },
  // 右侧按钮的选中事件
  selectedBtnClick: function(e) {
    var that = this;
    console.log(e)
    var tag = e.currentTarget.dataset.id;
    var imageUrl = "titleArr[" + tag + "].selectImageUrl"
    that.setData({
      cancellationReasons: tag + 1,
    })
    for (var i = 0; i < that.data.titleArr.length; i++) {
      var noImageUrl = "titleArr[" + i + "].selectImageUrl"
      if (tag == 5) {
        if (i == tag) {
          that.setData({
            otherHidden: false,
            [noImageUrl]: '/images/check_blue.png',
            content: that.data.otherContent,
            isCanCommite: true,
          })
        } else {
          that.setData({
            otherHidden: false,
            [noImageUrl]: '/images/check_gray.png',
          })
        }
      } else {
        if (i == tag) {
          that.setData({
            otherHidden: true,
            [imageUrl]: '/images/check_blue.png',
            content: that.data.titleArr[tag],
            isCanCommite: true,
          })
        } else {
          that.setData({
            otherHidden: true,
            [noImageUrl]: '/images/check_gray.png',
          })
        }
      }
    }
  },
  // 监听textArea输入的变化
  valueChange: function(e) {
    var that = this;
    that.setData({
      otherContent: e.detail.value,
      inputCount: e.detail.value.length,
    })
  },
  /// 点击确认提交按钮
  entrueCommitBtnClick: function() {
    var that = this;
    if (that.data.cancellationReasons == 6) {
      if (that.data.otherContent.length >= 10) {
        that.loadData()
      } else {
        toast.toastShow(this, '其他意见至少10个字')
      }
    } else {
      that.loadData()
    }
  },
  // 加载数据
  loadData() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.baseNet + 'orderInfo/confirmOrder?',
      data: {
        soiId: that.data.soiId,
        state: 6,
        username: that.data.username,
        otherReasons: that.data.otherContent,
        cancellationReasons: that.data.cancellationReasons,
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        console.log(res.data)
        if (res.data.messcode == "001") {
          wx.hideLoading()
          wx.showToast({
            title: '提交成功',
            duration: 500
          })

          setTimeout(function () {
            if (that.data.orderNum == 1) {
              // 从详情页进来的
              wx.navigateBack({
                delta: 2
              })
            } else {
              // 从订单页面进来的
              wx.navigateBack({
                delta: 1
              })
            }
          }, 1000)
        } else if (res.data.messcode == "002") {
          // 没有数据
        } else {
          // 服务器罢工
        }
      },
      fail() {
        wx.hideLoading()
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})